/**
 * Copyright (c) 2017-2017 All Rights Reserved.
 */
package com.zbest.zpay.gw.assemble.alipay;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Service;

import com.zbest.zpay.gw.dal.model.GwChannelPayConfigDO;
import com.zbest.zpay.gw.dal.model.GwCommunicationConfigDO;
import com.zbest.zpay.gw.model.MessageContext;
import com.zbest.zpay.masterdata.dal.model.ChannelConfigDO;
import com.zbest.zpay.service.gw.model.GwKeyEnum;

/**
 * 
 *
 * @author Administrator
 * @version $Id: CreateForexTradeAssemble.java, v 0.1 2017-4-25 下午11:06:09
 *          Administrator Exp $
 */
@Service("createForexTradeAssemble")
public class CreateForexTradeAssemble extends AbstractAlipayMessageAssemble {

    // 调用的接口名
    private static String SERVICE = "create_forex_trade";

    /**
     * @see com.zbest.zpay.gw.assemble.alipay.AbstractAlipayMessageAssemble#buildOriginParam(com.zbest.zpay.gw.model.MessageContext)
     */
    @Override
    protected Map<String, String> buildOriginParam(MessageContext context) {

        ChannelConfigDO channelConfigDO = channelConfigCache.get(context.getChannelId());
        GwChannelPayConfigDO channelPayConfigDO = channelPayConfigCache.get(channelConfigDO
            .getChannelSystemId());
        GwCommunicationConfigDO communicationConfigDO = communicationConfigCache.get(context
            .getChannelId());

        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String instSerialNo = context.getRequestData(GwKeyEnum.INST_SERIAL_NO.getCode());
        // 订单名称，必填
        String subject = context.getRequestData(GwKeyEnum.GOODS_TITLE.getCode());
        // 付款金额，必填
        String total_fee = context.getRequestData(GwKeyEnum.TOTAL_AMOUNT.getCode());
        // 商品描述，可空
        String body = context.getRequestData(GwKeyEnum.GOODS_DESC.getCode());
        // 币种，不可空
        String currency = context.getRequestData(GwKeyEnum.CURRENCY.getCode());
        // 回跳地址
        String returnUrl = context.getRequestData(GwKeyEnum.RETURN_URL.getCode());

        Map<String, String> sParaTemp = new HashMap<String, String>();
        sParaTemp.put("service", SERVICE);
        sParaTemp.put("partner", channelPayConfigDO.getPartnerId());
        sParaTemp.put("_input_charset", communicationConfigDO.getCharset());
        sParaTemp.put("notify_url", channelPayConfigDO.getNotifyUrl());
        sParaTemp.put("return_url", returnUrl);
        sParaTemp.put("out_trade_no", instSerialNo);
        sParaTemp.put("subject", subject);
        sParaTemp.put("total_fee", total_fee);
        sParaTemp.put("body", body);
        sParaTemp.put("currency", currency);
        return sParaTemp;
    }

    /**
     * @see com.zbest.zpay.gw.assemble.alipay.AbstractAlipayMessageAssemble#buildRequestForm(java.util.Map)
     */
    @Override
    protected String buildRequestForm(MessageContext context, Map<String, String> data) {

        GwCommunicationConfigDO communicationConfigDO = communicationConfigCache.get(context
            .getChannelId());

        StringBuffer sbHtml = new StringBuffer();

        sbHtml.append("<form id=\"alipaysubmit\" name=\"alipaysubmit\" action=\""
                      + communicationConfigDO.getRemoteAddress() + "_input_charset="
                      + communicationConfigDO.getCharset() + "\" method=\"" + "get" + "\">");

        for (Entry<String, String> entry : data.entrySet()) {
            String name = entry.getKey();
            String value = entry.getValue();

            sbHtml.append("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\"/>");
        }

        // submit按钮控件请不要含有name属性
        sbHtml.append("<input type=\"submit\" value=\"" + "确认"
                      + "\" style=\"display:none;\"></form>");
        sbHtml.append("<script>document.forms['alipaysubmit'].submit();</script>");

        return sbHtml.toString();
    }

}
